home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / AHDI / SYQUEST / SQHDX / PART.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  7.3 KB  |  347 lines

  1. /* part.c */
  2.  
  3.  
  4. #include "obdefs.h"
  5. #include "defs.h"
  6. #include "part.h"
  7. #include "bsl.h"
  8. #include "hdx.h"
  9. #include "addr.h"
  10.  
  11.  
  12. extern char sbuf[];
  13. extern int rebootp;
  14.  
  15. extern long bslsiz;
  16. extern long gbslsiz();
  17.  
  18. /*
  19.  * Complain about partition error.
  20.  *
  21.  */
  22. parterr(devno)
  23. int devno;
  24. {
  25.     char *pdev="X";
  26.     
  27.     *pdev = devno + '0';
  28.     (cantpart[PTNDEV].ob_spec)->te_ptext = pdev;
  29.     cantpart[PTNERROK].ob_state = NORMAL;
  30.     execform(cantpart);
  31.     return ERROR;
  32. }
  33.  
  34.  
  35. /*
  36.  * Fill in partition entry with default information
  37.  * and configuration values from the current "pr" wincap entry.
  38.  *
  39.  */
  40. fillpart(n, part)
  41. int n;
  42. PART *part;
  43. {
  44.     long num;
  45.     char *partid;
  46.     char *idstr = "XX";
  47.     char *wgetstr();
  48.  
  49.     idstr[1] = n + '0';
  50.  
  51.     part->p_flg = 0;        /* set defaults */
  52.     part->p_id[0] = 'G';
  53.     part->p_id[1] = 'E';
  54.     part->p_id[2] = 'M';
  55.     part->p_st = 0;
  56.     part->p_siz = 0;
  57.  
  58.     /* see if `pX' is mentioned */
  59.     *idstr = 'p';
  60.     if (wgetnum(idstr, &num) == OK)
  61.     {
  62.     part->p_siz = (LONG)(num / 512);
  63.     *idstr = 'f';
  64.     if (wgetnum(idstr, &num) == OK)
  65.         part->p_flg = (BYTE)num;
  66.         else part->p_flg = P_EXISTS;
  67.     *idstr = 'i';
  68.     if ((partid = wgetstr(idstr)) != NULL)
  69.         for (num = 0; num < 3; ++num)
  70.         part->p_id[num] = partid[num];
  71.     }
  72. }
  73.  
  74.  
  75. /*
  76.  * Extract partition structures from root sector.
  77.  *
  78.  */
  79. gpart(image, pinfo)
  80. char *image;
  81. PART *pinfo;
  82. {
  83.     register PART *rpart;
  84.     int i, j;
  85.  
  86.     rpart = &((RSECT *)(image + 0x200 - sizeof(RSECT)))->hd_p[0];
  87.  
  88.     for (i = 0; i < 4; ++i)
  89.     {
  90.     pinfo->p_flg = rpart->p_flg;
  91.     for (j = 0; j < 3; ++j)
  92.         pinfo->p_id[j] = rpart->p_id[j];
  93.     pinfo->p_st = rpart->p_st;
  94.     pinfo->p_siz = rpart->p_siz;
  95.  
  96.     ++pinfo;
  97.     ++rpart;
  98.     }
  99. }
  100.  
  101.  
  102. /*
  103.  * Install partition structures in root sector.
  104.  *
  105.  */
  106. spart(image, pinfo)
  107. char *image;
  108. PART *pinfo;
  109. {
  110.     register PART *rpart;
  111.     int i, j;
  112.  
  113.     rpart = &((RSECT *)(image + 0x200 - sizeof(RSECT)))->hd_p[0];
  114.  
  115.     for (i = 0; i < 4; ++i)
  116.     {
  117.     rpart->p_flg = pinfo->p_flg;    /* copy part struct */
  118.     for (j = 0; j < 3; ++j)
  119.         rpart->p_id[j] = pinfo->p_id[j];
  120.     rpart->p_st = pinfo->p_st;
  121.     rpart->p_siz = pinfo->p_siz;
  122.  
  123.     ++rpart;
  124.     ++pinfo;
  125.     }
  126. }
  127.  
  128.  
  129. /*
  130.  * Setup partitions on the disk;
  131.  * write boot sectors and zero FATs and root directories.
  132.  *
  133.  */
  134. dopart(physdev, pinfo)
  135. int physdev;
  136. register PART *pinfo;
  137. {
  138.     int i, ldev, ret;
  139.     SECTOR data;
  140.     char image[512], *devno="X";
  141.     long ndirs;
  142.     UWORD fatsiz;
  143.     BOOT *bs;
  144.  
  145.     if ((bslsiz = gbslsiz(physdev)) < 0L) {
  146.         if (bslsiz == ERROR)
  147.             err(rootread);
  148.         return ERROR;
  149.     }
  150.         
  151.     
  152.     for (i = 0; i < NPARTS; ++i, ++pinfo) {
  153.         
  154.         /* don't care if partition does not exist */
  155.     if (!(pinfo->p_flg & P_EXISTS)) {
  156.         continue;
  157.     }
  158.  
  159.     /*
  160.      * Compute boot sector parameters.
  161.      */
  162.     if (pinfo->p_siz >= 0x10000L) {        /* partition >=? 32Mb */
  163.         *devno = i + '0';
  164.         (part2big[BGPART].ob_spec)->te_ptext = devno;
  165.         part2big[BGPARTOK].ob_state = NORMAL;
  166.         execform(part2big);
  167.         return ERROR;
  168.     }
  169.  
  170.     /*
  171.      * Install entries in boot sector image;
  172.      * force sector checksum to zero (non-executable);
  173.      * write boot sector to media.
  174.      *
  175.       *    512 bytes/sector
  176.      *    2 or 4 sectors/cluster (partition > 16Mb has 4 spc)
  177.      *    1 reserved sector (for boot)
  178.      *    2 FATs
  179.      *    ... dir slots
  180.      *    ... # sectors
  181.      *    F8 means media is a hard disk
  182.      *    ... FAT size
  183.      *
  184.      */
  185.      
  186.     /* Make a clean boot sector */
  187.     fillbuf(image, 512L, 0L);
  188.     bs = (BOOT *)image;
  189.         
  190.         /* bytes per sector */
  191.     iw((UWORD *)&bs->b_bps[0], (UWORD)512);
  192.     
  193.     /* sectors per cluster */
  194.     if (pinfo->p_siz >= 0x8000L)    /* partition >= 16Mb */
  195.         bs->b_spc = 4;
  196.     else                /* partition < 16Mb */
  197.         bs->b_spc = 2;
  198.         
  199.     /* number of reserved sectors */
  200.     iw((UWORD *)&bs->b_res[0], (UWORD)1);
  201.     
  202.     /* number of FATs */
  203.     bs->b_nfats = 2;
  204.     
  205.  
  206.     /*
  207.      * Compute root directory size
  208.      * 256 entries, plus fudge on devs > 10Mb
  209.      */
  210.     if (pinfo->p_siz < 0x5000L) ndirs = 256;
  211.     else ndirs = pinfo->p_siz / 80;    /* 1 dir entry per 80 sectors */
  212.     ndirs = (ndirs + 15) & ~15;    /* round to nearest sector */
  213.     iw((UWORD *)&bs->b_ndirs[0], (UWORD)ndirs);
  214.     
  215.     /* number of sectors on media (partition) */
  216.     iw((UWORD *)&bs->b_nsects[0], (UWORD)pinfo->p_siz);
  217.     
  218.     /* media descriptor */
  219.     bs->b_media = 0xf8;
  220.  
  221.     /*--------------------------------------------------------------*
  222.      * Compute FAT size                        *
  223.      *                                *
  224.      * Num entries to map the entire partition            *
  225.      *    = partition's size in clusters                *
  226.      *    = partition's size in sectors / spc            *
  227.      *                                *
  228.      * Num entries in FAT                        *
  229.      *    = Num entries to map partition + reserved entries    *
  230.      *    = (partition's size in sectors / spc) + 2        *
  231.      *                                *
  232.      * Num sectors FAT occupies                    *
  233.      *    = Num entries in FAT / Num entries of FAT per sector    *
  234.      *    = Num entries in FAT / (512 / 2)    <16-bit FAT>    *
  235.      *    = ((partition's size in sectors / spc) + 2) / 256 + 1    *
  236.      *                        <+1 to round up>    *
  237.      *--------------------------------------------------------------*/
  238.     fatsiz = (((pinfo->p_siz / bs->b_spc) + 2) / 256) + 1;
  239.     iw((UWORD *)&bs->b_spf[0], (UWORD)fatsiz);
  240.  
  241.     /*
  242.      * Write partition's boot sector
  243.      */
  244.     forcesum((UWORD *)image, (UWORD)0);    /* force image checksum */
  245.     if ((ret = wrsects(physdev, 1, image, (SECTOR)pinfo->p_st)) != OK) {
  246.         if (tsterr(ret) != OK)
  247.             err(bootwrit);
  248.         return ERROR;
  249.     }
  250.  
  251.     /*
  252.      * Zero the partition
  253.      */
  254.     if ((ret = zerosect(physdev, (SECTOR)(pinfo->p_st+1),
  255.              (UWORD)(fatsiz*2 + ndirs/16))) != OK) {
  256.         if (tsterr(ret) != OK)
  257.             err(hdrwrite);
  258.         return ERROR;
  259.     }
  260.              
  261.     /*
  262.      * Make first 2 entries in FATs more IBM-like.
  263.      */
  264.     if ((ret = rdsects(physdev, 1, image, (SECTOR)(pinfo->p_st+1))) != 0) {
  265.         if (tsterr(ret) != OK)
  266.             err(fatread);
  267.         return ERROR;
  268.     }
  269.     *(UWORD *)&image[0] = 0xf8ff;
  270.     *(UWORD *)&image[2] = 0xffff;
  271.     if ((ret = wrsects(physdev, 1, image, (SECTOR)(pinfo->p_st+1))) != 0 ||
  272.         (ret = wrsects(physdev, 1, image, (SECTOR)(pinfo->p_st+1+fatsiz)))
  273.         != 0) {
  274.         if (tsterr(ret) != OK)
  275.             err(fatwrite);
  276.         return ERROR;
  277.     }
  278.  
  279.     /*
  280.      * Mark bad sectors recorded in the BSL into the FATs.
  281.      * Calculating parameters:
  282.      *    ldev - from physdev and i.
  283.      *    fat0 - always equals 1.
  284.      *    fatsiz - calculated above.
  285.      *    data - starts after the boot sector, 2 FATs and rootdir.
  286.      */
  287.     if (bslsiz > 0) {
  288.         if ((ldev = phys2log(physdev, i)) == ERROR)
  289.             return parterr(physdev);
  290.         data = (SECTOR)1 + (SECTOR)(fatsiz*2) + (SECTOR)(ndirs/16);
  291.         bsl2fat(ldev, (SECTOR)1, fatsiz, data, MEDIA);
  292.     }
  293.     }
  294.     return OK;
  295. }
  296.  
  297.  
  298. /*
  299.  * Force checksum of sector image to a value
  300.  */
  301. forcesum(image, sum)
  302. UWORD *image;
  303. UWORD sum;
  304. {
  305.     register int i;
  306.     register UWORD w;
  307.  
  308.     w = 0;
  309.     for (i = 0; i < 255; ++i)
  310.     w += *image++;
  311.     *image++ = sum - w;
  312. }
  313.  
  314.  
  315. /*
  316.  * Put word in memory in 8086 byte-reversed format.
  317.  *
  318.  */
  319. iw(wp, w)
  320. UWORD *wp;
  321. UWORD w;
  322. {
  323.     char *p;
  324.  
  325.     p = (char *)wp;
  326.     p[0] = (w & 0xff);
  327.     p[1] = ((w >> 8) & 0xff);
  328. }
  329.  
  330.  
  331. /*
  332.  * Get word in memory, from 8086 byte-reversed format.
  333.  *
  334.  */
  335. UWORD gw(wp, aw)
  336. UWORD *wp;
  337. UWORD *aw;
  338. {
  339.     char *p, *q;
  340.  
  341.     p = (char *)wp;
  342.     q = (char *)aw;
  343.     q[0] = p[1];
  344.     q[1] = p[0];
  345.     return *aw;
  346. }
  347.